define(function(require, exports, module) {
	require('avalon2');	
	require('resources/js/jquery.json-2.4.min.js');
	require('resources/js/avalon/mmRouter.js');
	
	var moduleMap=require('resources/template/portal/modules.js');
	
	var SidebarMenu=require('resources/template/portal/sidebarMenu.js');
	
	var Index=function(config){
		this.config=$.extend({},Index.DEFAULT,config);
		this.$div=$('#main-container');
		
		this.init();
	};
	
	Index.prototype={
		init:function(){
			var that=this;
			//0.导航栏渲染
			
			//1.菜单渲染
			var menuDeferred = $.Deferred()
			this.sidebarMenu=new SidebarMenu(this.$div,$.extend(this.config,{
				afterInit:function(){
					menuDeferred.resolve();
				}
			}));
			
			this.pageStack=[];//用来保存工作区页面层次深度{page模块,config}
			$.when(menuDeferred).done(function(){
				//2.面包屑
				that.initBreadCrumb();
				
				//3.工作区，页面切换效果:增加一个内嵌iframe
				that.curPage={};//当前页的组件对象
				that.$workspace=$('#workspace');
				
				//4.路由设置
				that.initRouter();
			});
		},
		toParentPage:function(){
			if(this.pageStack.length==0)
				return;
			
			var href=window.location.href;
			href=href.substring(0,href.lastIndexOf('/'));
			var menuId=href.substring(href.indexOf('#!'),href.length);
			menuId=menuId.substr(2);
			if(menuId.indexOf('/')==0)
				menuId=menuId.substr(1);
			if(this.sidebarMenu.menuItem[menuId])
			{
				window.location.href=href;
				return;
			}	
			
			//面包屑反应
			this.breadCrumbVM.data.pop();
			//地址改动
			window.location.href=href;
			//工作区反应
			this.curPage&&this.curPage.destroy&&this.curPage.destroy();
			var pageStackItem=this.pageStack.pop(),Page=pageStackItem.page;
			this.curPage=new Page(this.$workspace,$.extend({},this.config,pageStackItem.config),undefined,this);
		},
		toSubPage:function(module,config,moduleName){
			if(module==undefined)
				return;
			
			this.pageStack.push({
				page:this.curPageModule,
				config:this.curPage.config
			});
			
			//面包屑反应
			this.breadCrumbVM.data.push({
				href:'',
				name:moduleName,
				className:'',
				id:'',
				pid:''
			});
			//地址改动
			window.location.href=window.location.href+'/'+moduleName;
			
			//工作区反应
			this.curPage&&this.curPage.destroy&&this.curPage.destroy();
			var Page=module;
			this.curPageModule=Page;
			this.curPage=new Page(this.$workspace,$.extend({},this.config,config),undefined,this);
		},
		directPage:function(menuId,path,params){//工作区页面跳转
			var that=this;
			
			//菜单反应
			var menuPaths=this.sidebarMenu.selectMenuItem(menuId);
			
			//面包屑反应，设置this.breadCrumbVM.data
			this.breadCrumbVM.data.splice(0,this.breadCrumbVM.data.length);
			menuPaths&&$.map(menuPaths,function(menu){that.breadCrumbVM.data.unshift(menu)});
			
			//重置页面栈
			this.pageStack=[];
			
			//工作区反应
			this.curPage&&that.curPage.destroy&&that.curPage.destroy();
			var Page=moduleMap[menuId];
			this.curPageModule=Page;
			Page&&(this.curPage=new Page(this.$workspace,$.extend({},this.config,{
				menuId:menuId,
				path:path,
				params:params
			}),undefined,this));
		},
		initRouter:function(){//4.路由设置
			var that=this;
			//添加路由规则
			$.map(this.sidebarMenu.getHashs(),function(hash){
				avalon.router.add("/"+hash.replace(/\?.*/i,''), function () {
					that.directPage(this.path.substr(1),this.path,this.query);
				});
			});
			//启动路由监听
			avalon.history.start({
			    root: "/"
			});
		},
		initBreadCrumb:function(){
			var that=this;
			this.$breadcrumb=$('#breadcrumb');
			this.breadCrumbVM= avalon.define({
                $id: "breadcrumb",
                hasHref:function(index,el){
                	return (index!=that.breadCrumbVM.data.length-1)&&el.href!='#';
                },
                data:[]
            });
			avalon.scan(this.$breadcrumb.get(0),this.breadCrumbVM);
		}
	};	
	
	Index.DEFAULT={
			
	};
	
	return  Index;
});